#!/opt/ActiveTcl-8.5/bin/tclsh8.5

#if you want the converted files to be removed edit this variable to true

set global_rm false

source /home/muhammad/workspace/tayel-scripts-open-space/Tcl/UsefulPorcs.tcl
#package require Tk
#wm withdraw .



proc ConvertToJPG {directory {rm false}} {
    set file_list [GlobRecursed $directory *.bmp]
    puts "\"$directory\" has [llength $file_list] BMP Files"
    if {[llength $file_list] != 0} {
        puts Converting
    }
    foreach file $file_list {
        regexp {(.+)\.bmp} $file dummy file_jpg 
        append file_jpg .jpg
        puts -nonewline "Converting $file"
        exec convert $file $file_jpg
        if {$rm} {
            exec rm $file
        }
        puts "...Done!"
    }
}

if {(![info exists argc]) || ($argc == 0)} {
    ConvertToJPG [pwd] $global_rm
} elseif {$argc == 1} {
    ConvertToJPG [lindex $argv 0] $global_rm
    puts "All Done"
} elseif {$argc == 2} {
    ConvertToJPG [lindex $argv 0] [lindex $argv 1]
    puts "All Done"
} else {
    puts $argc
    puts $argv
}

